//
//  YSZMainController.swift
//  swiftweibo6
//
//  Created by ysz on 15/11/22.
//  Copyright © 2015年 user. All rights reserved.
//

import UIKit

class YSZMainController: UITabBarController {

    
     func composeClick()
    {
        print(__FUNCTION__)
    }
    override func viewDidLoad() {
        super.viewDidLoad()

        tabBar.tintColor = UIColor.orangeColor()
        let homeVC = YSZHomeViewController()
        addChildViewController(homeVC, title: "首页", imageName: "tabbar_home")
        
        let messageVC = YSZMessageViewController()
        addChildViewController(messageVC, title: "消息", imageName: "tabbar_message_center")
        
        addChildViewController(UIViewController())
        
        let discoverVC = YSZDiscoverViewController()
        addChildViewController(discoverVC, title: "发现", imageName: "tabbar_discover")
        
        let profile = YSZProfileViewController()
        addChildViewController(profile, title: "我", imageName: "tabbar_profile")
        
    }
    
    override func viewWillAppear(animated: Bool)
    {
        super.viewWillAppear(animated)
        let width = tabBar.bounds.size.width/5
        let frame = CGRect(x: width*2-5, y: 0, width: width+10, height: tabBar.bounds.size.height)
        composeButton.frame=frame
    }
    
    private func addChildViewController(controller: UIViewController, title: String, imageName: String)
    {
        controller.title = title
        controller.tabBarItem.image = UIImage(named: imageName)
        
        let hightimage = imageName + "_highlighted"
        UIImage(named: hightimage)?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
        
        addChildViewController(UINavigationController(rootViewController: controller))
        
    }
    
    lazy var composeButton: UIButton =
    {
        let btn = UIButton()
        
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
        btn.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
        btn.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
        
        self.tabBar.addSubview(btn)
        
        btn.addTarget(self, action: "composeClick", forControlEvents: UIControlEvents.TouchUpInside)
        return btn
        
    }()
}
